package tianrun.ziguan.api.calculate.config.dto.request.common.list;

import com.google.common.collect.Lists;
import org.springframework.util.Assert;
import tianrun.ziguan.api.calculate.config.dto.request.common.add.edit.QueryBody;
import tianrun.ziguan.api.calculate.config.utils.CollectionUtil;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class ListBody {
    @NotBlank(message = "tableName 不能为空！")
    private String tableName;
    private List<String> columns;
    @NotEmpty(message = "filterFields 不能为空！")
    private List<FilterField> filterFields;
    private List<SortedField> sortedFields;

    public static ListBody of(List<FilterField> filterFields, String tableName) {
        ListBody listBody = new ListBody();
        listBody.setFilterFields(filterFields);
        listBody.setTableName(tableName);
        listBody.setColumns(Lists.newArrayList());

        return listBody;
    }

    public static ListBody of(List<FilterField> filterFields, List<String> columns, String tableName) {
        ListBody listBody = new ListBody();
        listBody.setFilterFields(filterFields);
        listBody.setColumns(columns);
        listBody.setTableName(tableName);
        return listBody;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public List<String> getColumns() {
        return columns;
    }

    public void setColumns(List<String> columns) {
        this.columns = columns;
    }

    public List<FilterField> getFilterFields() {
        return filterFields;
    }

    public void setFilterFields(List<FilterField> filterFields) {
        this.filterFields = filterFields;
    }

    public List<SortedField> getSortedFields() {
        return sortedFields;
    }

    public void setSortedFields(List<SortedField> sortedFields) {
        this.sortedFields = sortedFields;
    }

    public QueryBody to(Set<String> set) {
        StringBuilder sb = new StringBuilder(" 1=1 ");
        filterFields.stream().filter(i -> set.contains(i.getFieldName())).forEach(i -> i.appendClause(sb));
        String whereCase = sb.toString();
        Assert.hasText(whereCase, "参数异常");
        List<String> _columns =CollectionUtil.isEmpty(this.columns)?Lists.newArrayList():this.columns.stream().filter(set::contains).collect(Collectors.toList());
        _columns = CollectionUtil.isEmpty(_columns) ? Lists.newArrayList(set) : _columns;
        List<SortedField> _sortedFields = CollectionUtil.isEmpty(sortedFields) ? Collections.emptyList() : sortedFields.stream().filter(i -> set.contains(i.getFieldName())).collect(Collectors.toList());

        QueryBody queryBody = new QueryBody();
        queryBody.setColumns(_columns);
        queryBody.setTableName(tableName);
        queryBody.setWhereCase(whereCase);
        queryBody.setSortedFields(_sortedFields);
        return queryBody;
    }
}
